class AdminController < ApplicationController
  def index
  end

  def get_reader_info
    reader_info = TbReader.find_by_barcode(params["reader"])
    if reader_info == nil
      return render :json => {:reader_status => false}
    end
    borrow_info = TbBorrow.where({:readerid=>reader_info[:readerid],:is_back=>false})
    if borrow_info == nil
      return render :json => {:reader_status => true, :borr_status => false, :reader_info => reader_info}
    end
    borrow_book_info = []
    borrow_info.each do |borrow_info|
      book_info = TbBookinfo.find_by_bookid(borrow_info[:bookid])
      book_typr = TbBooktype.find_by_booktypeid(book_info[:typeid])
      publish = TbPublish.find_by_ISBN(book_info[:ISBN])
      data ={:book_name => book_info[:bookname], :borrow_time => borrow_info[:borrowTime],
             :shouldbacktime => borrow_info[:shouldbacktime], :book_type => book_typr[:Typename],
             :book_money => book_info[:price], :publish => publish[:pubname], :borrow_id => borrow_info[:id]}
      borrow_book_info.push(data)
    end

    render :json => {:reader_status => true, :borr_status => true, :borrow_book_info => borrow_book_info, :reader_info => reader_info}
  end

  def get_reader_borrow_info
    p params
    reader_info = TbReader.find_by_barcode(params["reader_code"])
    render :json => reader_info
  end

  def borrow_book
    reader = TbReader.find_by_barcode(params["user_barcode"])
    book_info = TbBookinfo.find_by_barcode(params["book_barcode"])
    book_typr = TbBooktype.find_by_booktypeid(book_info[:typeid])
    publish = TbPublish.find_by_ISBN(book_info[:ISBN])
    borrow_info_data = {
        :borrow_id => Time.now.to_i,
        :readerid => reader[:readerid],
        :bookid => book_info[:bookid],
        :bookname => book_info[:bookname],
        :borrowTime => Time.now,
        :shouldbacktime => Time.now,
        :is_back => false,
        :remark => 0,
        :fine => 0
    }

    borrow_info = TbBorrow.new(borrow_info_data)
    borrow_info.save
    render :json => {:book_name => book_info[:bookname], :borrow_time => borrow_info[:borrowTime],
                     :shouldbacktime => borrow_info[:shouldbacktime], :book_type => book_typr[:Typename],
                     :book_money => book_info[:price], :publish => publish[:pubname]}
  end


  def return_book
    p "---------------------------"
    p params
    borrow_info = TbBorrow.find_by(params["borrow_id"])
    borrow_info.update(:is_back => true)

    render :json => {:status => true,:borrow_id =>params["borrow_id"]}

  end
end
